///************************************************************************
/// <copyrigth>2018-2019 Corporation.All Rights Reserved</copyrigth>
/// <author>tangyingzhong</author>
/// <contact>tangyz114987@outlook.com</contact>
/// <version>v1.0.0</version>
/// <describe>
/// Shared memory for all the processes in one computer
///</describe>
/// <date>2019/7/16</date>
///***********************************************************************
#ifndef SHAREDMEMORY_H
#define SHAREDMEMORY_H

#include <string>
#include "boost/interprocess/shared_memory_object.hpp"
#include "boost/interprocess/mapped_region.hpp"

namespace bip = boost::interprocess;

namespace SystemTool
{
	namespace Buffer
	{
		enum SmOpenMode
		{
			// Create only
			SM_CREATE_ONLY = 0,

			// Open only 
			SM_OPEN_ONLY
		};

		class SharedMemory
		{
		public:
			typedef void None;
			typedef void* Object;
			typedef bool Boolean;
			typedef char Byte;
			typedef Byte* ByteArray;
			typedef int Length;
			typedef Length Offset;

		public:
			// Construct the SharedMemory
			SharedMemory();

			// Detructe the SharedMemory
			~SharedMemory();

		private:
			// Forbid the object copying
			SharedMemory(const SharedMemory& other) {	}

			// Forbid the object copying
			SharedMemory& operator=(const SharedMemory& other) { return *this; }

		public:
			// Configure the memory
			// (Len must be one of 64K ,2*64K,4*64KIt can be called one-level cache,two-level cache and four -level cache)
			None Configure(const std::string strMemoryName, const int32_t iMemorySize);

			// Is the shared memory opened or not
			Boolean IsOpen();

			// Open the managed memory
			Boolean Open(SmOpenMode eOpenMode);

			// Close the managed memory(note: if you destory the share memory, you must open it with create mode at next time)
			Boolean Close();

			// Clear the memory
			Boolean Clear();

			// Read the common memory. 
			Boolean Read(ByteArray buffer, Length offset, Length len);

			// Write to the common memory
			Boolean Write(ByteArray buffer, Length offset, Length len);

		private:
			// Initialize the shared memory
			None Initialize();

			// Destory the shared memory
			None Destory();

			// Destory the share memory
			Boolean DestoryMemory();

		private:
			// Get the managed memory
			inline bip::shared_memory_object* GetManagedMemory() const
			{
				return m_pManagedMemory;
			}

			// Set the managed memory
			inline None SetManagedMemory(bip::shared_memory_object* pManagedMemory)
			{
				m_pManagedMemory = pManagedMemory;
			}

			// Get the Name
			inline std::string GetMemoyName() const
			{
				return m_strMemoryName;
			}

			// Set the Name
			inline None SetMemoryName(std::string strMemoryName)
			{
				m_strMemoryName = strMemoryName;
			}

			// Get the memory size
			inline int32_t GetMemorySize() const
			{
				return m_iMemorySize;
			}

			// Set the memory size
			inline None SetMemorySize(int32_t iMemorySize)
			{
				m_iMemorySize = iMemorySize;
			}

			// Get the disposed
			inline Boolean GetDisposed() const
			{
				return m_Disposed;
			}

			// Set the disposed	
			inline None SetDisposed(Boolean bDisposed)
			{
				m_Disposed = bDisposed;
			}

		private:
			// Share memory segement
			bip::shared_memory_object* m_pManagedMemory;

			// The name of the memory
			std::string m_strMemoryName;

			// The size of the sheared memory
			int32_t m_iMemorySize;

			// Disposed status
			Boolean m_Disposed;
		};
	}
}

#endif // SHAREDMEMORY_H
